#include "mainwindow.h"
#include "stitlebar.h"
#include "sorderview.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QDebug>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    setupUI();
    setupConnections();
}

MainWindow::~MainWindow() {}

void MainWindow::setupUI(){
#ifdef QT_DEBUG
    setFixedSize(1024, 800);
#else
    showFullScreen();
#endif
    setAttribute(Qt::WA_TranslucentBackground);
    setWindowFlags(Qt::FramelessWindowHint);

    //标题栏
    titleBar = new STitleBar(this);
    titleBar->SetTitle(tr("简单收银系统"));

    //主页面
    stackedWidget = new QStackedWidget;
    orderView = new SOrderView;
    settings = new QLabel;
    menuPage = new MenuPage;
    settings->setText("设置");
    stackedWidget->addWidget(orderView);
    stackedWidget->addWidget(menuPage);
    stackedWidget->addWidget(settings);

    //主菜单
    mainButtonList = new MainButtonList;
    QList<MainButtonContent> mainButtons =
        {
         {tr("点单"),"://icons/order.svg", orderView},
         {tr("账单"),"://icons/order.png", settings},
         {tr("菜单"),"://icons/order.png", menuPage},
         {tr("报表"),"://icons/order.png", nullptr},
         {tr("设置"),"://icons/order.png", nullptr},
         };
    mainButtonList->SetContent(mainButtons);


    //布局
    QHBoxLayout *mainContentLayout = new QHBoxLayout;
    mainContentLayout->addWidget(mainButtonList);
    mainContentLayout->addWidget(stackedWidget);
    mainContentLayout->addStretch();

    QVBoxLayout * mainLayout = new QVBoxLayout;
    mainLayout->setContentsMargins(0, 0, 0, 0);
    mainLayout->addWidget(titleBar);
    mainLayout->addLayout(mainContentLayout);


    QWidget* centralWidget = new QWidget(this);
    centralWidget->setObjectName("mainWindow");
    centralWidget->setContentsMargins(5, 5, 5, 5);
    centralWidget->setStyleSheet(R"(
        #mainWindow {
            background: #eeeeee;
            border-radius: 8px;
            border: 2px solid #409EFF;
            padding: 2px;
        }
    )");


    centralWidget->setLayout(mainLayout);
    setCentralWidget(centralWidget);
};

void MainWindow::applyStyles(){

};

void MainWindow::setupConnections(){
    connect(titleBar, &STitleBar::closeWindow, this, [=]{
        close();
    });

    connect(mainButtonList, &MainButtonList::currentItemChanged, [=](QListWidgetItem* current, QListWidgetItem* previous){
        QWidget* page = current->data(Qt::UserRole).value<QWidget*>();
        // if(current) qDebug() << "当前选中:" << current->text();
        // if(previous) qDebug() << "之前选中:" << previous->text();
        stackedWidget->setCurrentWidget(page);
    });
};
